#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <vector>

using namespace std;

// 递归列出所有目录及文件
void recursion_scan_dir_file(char *dir, int depth, vector<string>& fileNames, char *path)
{
        char wholePath[128] = {0};
        DIR *p_dir = NULL;
        struct dirent *p_entry = NULL;
        struct stat statbuf;
        // 如果是初始目录是不加/ 不然会导致相对路径出错
        if (depth == 1){
            sprintf(wholePath, "%s%s", path, dir);
        }else{
            sprintf(wholePath, "%s/%s", path, dir);
        }
        
        if((p_dir = opendir(dir)) == NULL)
        {
                printf("can't open dir.\n");
                return;
        }
        
        chdir (dir);
        // 获取下一级目录信息
        while(NULL != (p_entry = readdir(p_dir))) {
                // 获取下一级成员属性 
                lstat(p_entry->d_name, &statbuf);
                // 判断下一级成员是否是目录   
                if(S_IFDIR & statbuf.st_mode) {      
                        // 判断是不是.或者..
                        if (strcmp(".", p_entry->d_name) == 0 || strcmp("..", p_entry->d_name) == 0)
                                continue;
                        // printf("%*s%s/\n", depth, "", p_entry->d_name);
                        // 扫描下一级目录的内容
                        recursion_scan_dir_file(p_entry->d_name, depth+4, fileNames, wholePath); 
                } else {
                        // printf("%*s%s\n", depth, "", p_entry->d_name);  
                        // 不是目录时，合并完整目录，并将文件名写入vector中
                        char wholePathName[128] = {0};
                        sprintf(wholePathName, "%s/%s", wholePath, p_entry->d_name);
                        fileNames.push_back(wholePathName);
                }
        }
        chdir(".."); // 回到上级目录
        closedir(p_dir);
}

//列出一个目录下所有文件
void scan_one_dir( const char * dir_name, vector<string>& fileNames)
{
    
    if(NULL == dir_name)
    {
        cout<<" dir_name is null ! "<<endl;
        return;
    }

    struct stat s;
    lstat( dir_name , &s );
    if(!S_ISDIR(s.st_mode)) //判断是不是文件件
    {
        return;
    }

    struct dirent * filename;
    DIR * dir;
    dir = opendir(dir_name);
    if(NULL == dir)
    {
        return;
    }

    int iName=0;
    while((filename = readdir(dir)) != NULL)
    {
        if( strcmp(filename->d_name, "." ) == 0 || strcmp( filename->d_name , "..") == 0)//如果是上级目录或者当前目录则跳过
            continue;

        char wholePath[128] = {0};
        sprintf(wholePath, "%s/%s", dir_name, filename->d_name);
        fileNames.push_back(wholePath);

        // cout << "wholePath= " << wholePath << endl;
    }
}

// main(int argc, char* argv[])
// {
//     vector<string> fileNames;
//     // scan_one_dir(argv[1], fileNames);
//     // for (const auto &ph : fileNames) {
// 	// 	std::cout << ph << "\n";
// 	// }
//     recursion_scan_dir_file(argv[1], 1, fileNames, "");
//     for (const auto &ph : fileNames) {
// 		std::cout << ph << "\n";
// 	}
//     return 0;
// }
